package com.example.administrator.myapplication;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    ArrayList<Food> foodList ;

    ListView mLvFood;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLvFood = findViewById(R.id.Lv_food);

        foodList = readFoodList();
        for (int i = 0; i < foodList.size(); i++) {
            if(foodList.get(i).foodId == 3){
                Log.e(TAG, "onCreate: "+foodList.get(i).foodName );
            }
        }
        MyAdapter adapter = new MyAdapter();
        mLvFood.setAdapter(adapter);
    }

  class MyAdapter extends BaseAdapter{

      @Override
      public int getCount() {
          return foodList.size();
      }

      @Override
      public Object getItem(int position) {
          return null;
      }

      @Override
      public long getItemId(int position) {
          return 0;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {

          Food food = foodList.get(position);

          View view =View.inflate(MainActivity.this,R.layout.list_item_layout,null);



          TextView tvFoodName=view.findViewById(R.id.tv_name);

          tvFoodName.setText(food.foodName);

          ImageView ivFoodpic = view.findViewById(R.id.iv_food_pic);
          ivFoodpic.setImageResource(getResourceId(food.foodPic));



          return view;
      }
  }
     int getResourceId(String picName){
        String substring = picName.substring(0,picName.indexOf('.'));
        int mipmap = getResources().getIdentifier(substring,"mipmap",getPackageName());
        return mipmap;
     }



    class  Food {
        /*
         "food_id": 1,
            "food_name": "桂林米粉",
            "food_price": 15,
            "food_desc": "以其独特的风味远近闻名。其做工考究，先将上好的早籼米磨成浆，装袋滤干，揣成粉团煮熟后压榨成圆根或片状即成。圆的称米粉，片状的称切粉，通称米粉，其特点是洁白、细嫩、软滑、爽口。",
            "food_pic": "guilin.jpg",
            "food_hot": 1,
            "food_sour": 1,
            "food_fish": 0
         */
        public int foodId;
        public String foodName;
        public int foodPrice;
        public String foodDesc;
        public String foodPic;
        public int foodHot;
        public int foodSour;
        public int foodFish;

        public Food() {
        }

        public Food(int foodId, String foodName, int foodPrice, String foodDesc, String foodPic, int foodHot, int foodSour, int foodFish) {
            this.foodId = foodId;
            this.foodName = foodName;
            this.foodPrice = foodPrice;
            this.foodDesc = foodDesc;
            this.foodPic = foodPic;
            this.foodHot = foodHot;
            this.foodSour = foodSour;
            this.foodFish = foodFish;
        }

    }


    ArrayList readFoodList(){
        ArrayList<Food> foodArrayList = new ArrayList<>();//定义一个ArrayList来保存数据
        try {

            String jsonStr = readFile();// 读取文件中的字符串

            getArrayList(foodArrayList, jsonStr); // 将字符串转换为ArrayList

            return foodArrayList;

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (JSONException e) {
            e.printStackTrace();
            return  null;
        }
    }

    private ArrayList getArrayList(ArrayList<Food> foodArrayList, String jsonStr) throws JSONException {
        JSONObject jsonObject = new JSONObject(jsonStr);// 讲字符串转换为json对象
        JSONArray rows_detail = jsonObject.optJSONArray("ROWS_DETAIL");
        for (int i = 0; i < rows_detail.length(); i++) {
            //遍历 jsonarray中的数据，将其结构修改为ArrayList
            JSONObject jsonObject1 = rows_detail.optJSONObject(i); // 得到第i项food json对象

            Food food =new Food();
            food.foodId = jsonObject1.getInt("food_id");
            food.foodName = jsonObject1.getString("food_name");
            food.foodPic = jsonObject1.getString("food_pic");
            //……
            foodArrayList.add(food);// 讲food对象添加到Arraylist中
        }
        return foodArrayList;//返回链表
    }

    private String readFile() throws IOException {
        InputStream inputStream = getResources().openRawResource(R.raw.foodlsit);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes);
        inputStream.close();
        String str = new String(bytes);
        return str;
    }
}

